/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "ui_extension_ability_connection.h"
#include "ability_manager_client.h"
#include "iam_logger.h"

#define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA

namespace OHOS {
namespace UserIam {
namespace UserAuth {
#define PARCEL_WRITE_HELPER_NORET(parcel, type, value) \
    do { \
        bool ret = parcel.Write##type((value)); \
        if (!ret) { \
            IAM_LOGE("write value failed!"); \
            return; \
        } \
    } while (0)
void UIExtensionAbilityConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
    const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
{
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    IAM_LOGI("on ability connected");

    // if (!data.WriteInterfaceToken(remoteObject->GetDescriptor())) {
    //     IAM_LOGE("failed to write descriptor");
    //     // return;IAM_LOGI
    // }

    // key的个数
    IAM_LOGI("change WriteString to WriteString16");
    data.WriteInt32(3);
    data.WriteString16(u"bundleName");
    // Fill in the bundleName that needs to pull up the UIability
    data.WriteString16(u"com.ohos.useriam.authwidget");
    data.WriteString16(u"abilityName");
    // Fill in the abilityName of the facility to be raised
    data.WriteString16(u"userauthuiextensionability");
    data.WriteString16(u"parameters");
    data.WriteString16(Str8ToStr16(commandStr_));

    // int dialogType = businessAbilityRouter.BussinessType.COMMON_DIALOG; // 2.1弹窗类型 ??
    // int dialogType = 1;
    // data.WriteString16(u"dailogType");
    // data.WriteInt32(dialogType);


    // bool isMarkUIHidden = true; // 2.2设置防截屏参数
    // wantParams.SetParam("ohos.ability.params.markUIHidden", AAFwk::Boolean::Box(isMarkUIHidden));
    // data.WriteString16(u"ohos.ability.params.markUIHidden");
    // data.WriteBool(AAFwk::Boolean::Box(isMarkUIHidden));
    // data.WriteBool(true);
    // data.WriteString16(u"userIamCmdData");
    // data.WriteString16(Str8ToStr16(commandStr_));


    // PARCEL_WRITE_HELPER_NORET(data, Parcelable, &element);
    // PARCEL_WRITE_HELPER_NORET(data, RemoteObject, remoteObject);
    // PARCEL_WRITE_HELPER_NORET(data, Int32, resultCode);
    // int32_t errCode = remoteObject->SendRequest(AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
    int32_t errCode = remoteObject->SendRequest(1, data, reply, option);
    IAM_LOGI("AbilityConnectionWrapperProxy::OnAbilityConnectDone result %{public}d", errCode);
}

void UIExtensionAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element,
    int32_t resultCode)
{
    IAM_LOGI("on ability disconnected");
}
} // namespace UserAuth
} // namespace UserIam
} // namespace OHOS